package com.cloud.config;

import feign.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignLoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignLogConfig {

    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public FeignLoggerFactory infoFeignLoggerFactory() {
        return new FeignLoggerFactoryImpl();
    }

    private static class FeignLoggerFactoryImpl implements FeignLoggerFactory {

        @Override
        public Logger create(Class<?> type) {
            return new InfoFeignLogger(LoggerFactory.getLogger(type));
        }
    }

    private static class InfoFeignLogger extends Logger {

        private final org.slf4j.Logger logger;

        public InfoFeignLogger(org.slf4j.Logger logger) {
            this.logger = logger;
        }

        @Override
        protected void log(String configKey, String format, Object... objects) {
            if (logger.isInfoEnabled()) {
                logger.info(String.format(methodTag(configKey) + format, objects));
            }
        }
    }
}